$(function () {
    obj.init();
});

var obj = new function () {
    return {
        goodsForm: null,
        modify: null,
        id: null,
        init: function () {
            this.id = xhTool.getQuery('id');
            this.goodsForm = new xhForm($('#xh_form'));
            this.funObj().checkExistData();
            this.goodsForm.parse().bindRequired($('#bt_modal_confirm',parent.document));
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().saveData();
            });
        },
        funObj: function () {
            var form = this.goodsForm;
            var $this = this;
            return {
                //修改的话条件数据
                checkExistData: function () {
                    if (goodsInfo !== 0){
                        $.each(goodsInfo, function(key, value){
                            if (key === 'sGoodsName'){
                                $('#goodsName').val(value);
                            }
                            if (key === 'iGoodsType'){
                                $('#goodsTypeId').val(value);
                            }
                            if (key === 'iUnit'){
                                $('#unit').val(value);
                            }
                            if (key === 'iType'){
                                $('#type').val(value);
                            }
                            if (key === 'sRemark'){
                                $('#remark').val(value);
                            }
                        });
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        $this.modify = 2;//修改
                    } else {
                        $this.modify = 1;//创建
                    }
                },
                saveData: function () {
                    var goodsName = $('#goodsName').val();
                    var goodsTypeId = $('#goodsTypeId').val();
                    var unit = $('#unit').val();
                    var type = $('#type').val();
                    var remark = $('#remark').val();
                    if ((goodsName === null || goodsName === '') || (goodsTypeId === null || goodsTypeId === '') || (unit === null || unit === '')){
                        window.top.xhTop.showError('请填写完整信息');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    if ($this.id !== undefined && $this.id !== null && $this.id !== ''){
                        id = $this.id
                    } else {
                        id = null;
                    }
                    xhExtra.ajaxRequest('lm/goods/saveGoodsData', {id:id, isCreate:$this.modify, goodsName:goodsName, goodsTypeId:goodsTypeId, unit:unit, type:type, remark:remark},
                        {success: true,check:false},function (res) {
                            if(res.code === 200){
                                window.top.swal('成功',res.msg,'success');
                                window.top.xhTop.closeModal();
                                window.top.xhTop.getActiveFrame().obj.dataGrid_2.reload();
                            } else {
                                window.top.xhTop.showError(res.error);
                                xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                            }
                        });
                }
            }
        }
    }
};